package com.jinke.api.modules.ums.task;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.modules.app.enums.ChannelPlatform;
import com.jinke.api.modules.app.model.Order;
import com.jinke.api.modules.app.service.OrderService;
import com.jinke.api.modules.app.service.SysCityService;
import com.jinke.api.modules.app.service.SysStateService;
import com.jinke.api.modules.third.ezeeship.model.response.CreateLabelResponse;
import com.jinke.api.modules.third.ezeeship.service.EzeeShipService;
import com.jinke.api.modules.ums.model.Channel;
import com.jinke.api.modules.ums.service.ChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
@Profile({"prod"})
public class OrderTask {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private EzeeShipService ezeeShipService;
    @Autowired
    private SysStateService sysStateService;
    @Autowired
    private SysCityService sysCityService;


    /**
     * 每天凌晨一点取订单表中状态为1，2的，如果是ezeeship,更新最新状态
     * @throws Exception
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void test() throws Exception {
        // 更新订单状态
        List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>()
                .isNotNull(Order::getObjectId)
                .in(Order::getStatus, Arrays.asList(1, 2)));

        if (CollectionUtil.isNotEmpty(orders)) {

            for (Order order : orders) {

                Channel channel = channelService.getOne(new LambdaQueryWrapper<Channel>().eq(Channel::getChannelCode, order.getServiceCode()));
                if (channel == null) {
                    continue;
                }

                // 只更新ezeeship
                if (ChannelPlatform.EZEE_SHIP.getValue() == (channel.getChannelPlatformId())) {
                    CommonResult label = ezeeShipService.getLabel(order.getObjectId(), order.getUserId());
                    Object data = label.getData();
                    if (data instanceof CreateLabelResponse.Data) {
                        int status = ((CreateLabelResponse.Data) data).getStatus();
                        if (order.getStatus() != status) {
                            orderService.update(new LambdaUpdateWrapper<Order>().eq(Order::getId, order.getId()).set(Order::getStatus, status));
                        }
                    }
                }
            }
        }


    }

//    @Scheduled(cron = "*/30 * * * * ?")
//    public void test1() throws Exception {
//        String filePath = "D:\\Download\\jink_003H_states.json";
//
//        // 从文件中读取 JSON 数据
//        String json = FileUtil.readUtf8String(filePath);
//        List<SysState> sysStates = new ArrayList<>();
//
//        JSONObject jsonobj = JSONUtil.parseObj(json);
//        jsonobj.forEach(x->{
//            SysState sysState = new SysState();
//            sysState.setStateCode(x.getKey());
//            JSONObject value = (JSONObject)x.getValue();
//            sysState.setStateName(value.getStr("name"));
//            JSONObject cities = value.getJSONObject("cities");
//            sysStateService.save(sysState);
//            List<SysCity> sysCities = new ArrayList<>();
//            cities.forEach(c->{
//
//                JSONArray jsonArray = (JSONArray)c.getValue();
//                for(Integer zipCode : jsonArray.toList(Integer.class)){
//                    SysCity sysCity = new SysCity();
//                    sysCity.setCityName(c.getKey());
//                    sysCity.setZipCode(zipCode);
//                    sysCity.setStateId(sysState.getId());
//                    sysCities.add(sysCity);
//                }
//            });
//            sysCityService.saveBatch(sysCities);
//
//        });
//        System.out.println(sysStates.size());
//
//
//
//    }
}
